package com.lanou.beautifulwardrobe.cart.adapter;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entity.greendao.ProductEntity;

import java.util.List;

/**
 * Created by dllo on 15/11/9.
 */
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
    private List<ProductEntity> datas;
    private VolleySingleton volleySingleton=VolleySingleton.getInstance();

    public void addData(List<ProductEntity> datas) {
        notifyDataSetChanged();
    }

    @Override
    public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_item, null);
        return new CartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CartViewHolder holder, int position) {
        if(datas!=null&&datas.size()>0){
            holder.cartDecription.setText(datas.get(position).getBrandName());
            holder.cartBrand.setText(datas.get(position).getBrandName());
            holder.cartPrice.setText(datas.get(position).getPrice());
            holder.cartSmellPrice.setText(datas.get(position).getSmellPrice());
            holder.cartPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            volleySingleton.getImageResuest(datas.get(position).getPic_url(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    holder.cartImage.setImageBitmap(response);
                }
            });
            
        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    class CartViewHolder extends RecyclerView.ViewHolder {
        private TextView cartDecription, cartSmellPrice, cartPrice, cartBrand;
        private ImageView cartImage;

        public CartViewHolder(View view) {
            super(view);
            cartDecription = (TextView) view.findViewById(R.id.cartDecription);
            cartSmellPrice = (TextView) view.findViewById(R.id.cartSmellPrice);
            cartPrice = (TextView) view.findViewById(R.id.cartPrice);
            cartBrand = (TextView) view.findViewById(R.id.cartBrand);
            cartImage = (ImageView) view.findViewById(R.id.cartImage);
        }
    }
}
